﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework;
using System.IO;
using CNVP.JShop.UI;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin
{
    public partial class PubAds : AdminPage
    {
        BLL.CNVP_Ads bll = new BLL.CNVP_Ads();
        Model.CNVP_Ads model = new Model.CNVP_Ads();
        public int id = 0;
        public string picOriginal = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 139)) return;//判断页面权限
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]) && Public.IsNumber(Request.QueryString["ID"]))
            {
                id = Convert.ToInt32(Request.QueryString["ID"]);
            }
            if (!IsPostBack)
            {
                ddlAdClass();
                if (id > 0)
                {
                    lbTitle.Text = " > 修改";
                    DataInfo(id);
                }
                else
                {
                    int AdClassID = 0;
                    if (!string.IsNullOrEmpty(Request.QueryString["AdClassID"]) && Public.IsNumber(Request.QueryString["AdClassID"].ToString()))
                    {
                        AdClassID = Convert.ToInt32(Request.QueryString["AdClassID"]);
                    }
                    if (AdClassID > 0)
                    {
                        ddlAdClassID.SelectedValue = AdClassID.ToString();
                    }
                    lbTitle.Text = " > 添加";
                }
                rblIsType.Attributes.Add("onClick", "IsType()");
            }
        }

        private void ddlAdClass()
        {
            IList<Model.CNVP_Column> model = new BLL.CNVP_Column().GetAll_CNVP_Column("ColumnType = 8 and IsShow=1 and ID!=105 order by OrderID");
            foreach (Model.CNVP_Column c in model)
            {
                ddlAdClassID.Items.Add(new ListItem(c.ColumnName, c.ID.ToString()));
            }
        }
        public void DataInfo(int id)
        {
            model = bll.GetSingle_CNVP_AdsById(id);
            if (model != null)
            {
                rblIsType.SelectedValue = model.IsType.ToString();
                txtAdName.Text = model.AdName;
                txtAdAddress.Text = model.AdAddress;
                if (model.AdPic != "")
                {
                    imgPic.Visible = true;
                    string[] AdPicArray = model.AdPic.Split(Convert.ToChar("|"));
                    if (AdPicArray.Length > 1)
                    {
                        imgPic.ImageUrl = Public.GetFirst(model.AdPic);
                        picOriginal = Public.GetSecond(model.AdPic);
                    }
                    else
                    {
                        imgPic.ImageUrl = model.AdPic;
                        picOriginal = model.AdPic;
                    }
                }
                txtPicOldName.Text = model.AdPic;
                txtAdDetail.Value = model.AdText;
                rblIsShow.SelectedValue = model.IsShow.ToString();
                txtOrderID.Text = model.OrderID.ToString();
                ddlAdClassID.SelectedValue = model.AdClassID.ToString();
            }
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
                        if (txtAdName.Text.Trim() == "")
            {
                BasePage.MessageBoxGoHistory("友链名称不能为空！");
                return;
            }
            string newName = "";
            string newName_Small = "";
            string PicName = "";
            string root = Server.MapPath("/UploadFile/Product/");
            string strRoot = "/UploadFile/Product/";
            if (id > 0)
            {
                #region 修改
                if (this.fileUp.PostedFile.ContentLength != 0)
                {
                    string fileName = fileUp.PostedFile.FileName;
                    string postfix = fileName.Substring(fileName.LastIndexOf("."));
                    if (postfix.ToLower() != ".jpg" && postfix.ToLower() != ".bmp" && postfix.ToLower() != ".gif" && postfix.ToLower() != ".jpeg" && postfix.ToLower() != ".png")
                    {
                        BasePage.MessageBox("上传的图片必须为.jpg或.bmp或.gif或.jpeg或.png！");
                        return;
                    }
                    newName = Public.getRandomFilename(root) + "Ad" + postfix;
                    fileUp.PostedFile.SaveAs(root + "\\" + newName);
                    newName_Small = Public.parseFn(newName, "_400px300px");
                    Public.MakeThumbnail(root + "\\" + newName, root + "\\" + newName_Small, 400, 300, "W");
                    PicName = strRoot + newName_Small + "|" + strRoot + newName;
                }
                else
                {
                    PicName = txtPicOldName.Text;
                }
                int rows = new BLL.CNVP_Ads().Update_CNVP_Ads(
                       new Model.CNVP_Ads()
                       {
                           ID = id,
                           IsType=byte.Parse(rblIsType.SelectedValue),
                           AdName = txtAdName.Text.Trim(),
                           AdAddress = txtAdAddress.Text.Trim(),
                           AdPic = PicName,
                           AdText = txtAdDetail.Value.Trim(),
                           IsShow = byte.Parse(rblIsShow.SelectedValue),
                           OrderID = int.Parse(txtOrderID.Text.Trim() == "" ? "0" : txtOrderID.Text.Trim()),
                           AdClassID = int.Parse(ddlAdClassID.SelectedValue)
                       }

               );
                if (rows == 1)
                {
                    if (this.fileUp.PostedFile.ContentLength != 0)
                    {
                        BasePage.DeleteFile(root + "\\" + Public.GetFirst(txtPicOldName.Text.Trim()));
                        BasePage.DeleteFile(root + "\\" + Public.GetSecond(txtPicOldName.Text.Trim()));
                    }
                    txtPicOldName.Text = PicName;
                    BasePage.MessageBox("修改成功！", "AdList.aspx?Page=" + Request.QueryString["Page"] + "");
                }
                else
                { BasePage.MessageBoxGoHistory("修改失败！"); }
                #endregion
            }
            else
            {
                #region 添加
                if (this.fileUp.PostedFile.ContentLength != 0)
                {
                    string fileName = fileUp.PostedFile.FileName;
                    string postfix = fileName.Substring(fileName.LastIndexOf("."));
                    if (postfix.ToLower() != ".jpg" && postfix.ToLower() != ".bmp" && postfix.ToLower() != ".gif" && postfix.ToLower() != ".jpeg" && postfix.ToLower() != ".png")
                    {
                        BasePage.MessageBox("上传的图片必须为.jpg或.bmp或.gif或.jpeg或.png！");
                        return;
                    }
                    newName = Public.getRandomFilename(root) + "Ad" + postfix;
                    fileUp.PostedFile.SaveAs(root + "\\" + newName);
                    newName_Small = Public.parseFn(newName, "_400px300px");
                    Public.MakeThumbnail(root + "\\" + newName, root + "\\" + newName_Small, 400, 300, "W");
                    PicName = strRoot + newName_Small + "|" + strRoot + newName;
                }
                else
                {
                    PicName = txtPicOldName.Text.Trim();
                }
                int rows = new BLL.CNVP_Ads().Add_CNVP_Ads(
                    new Model.CNVP_Ads()
                    {
                        IsType = byte.Parse(rblIsType.SelectedValue),
                        AdName = txtAdName.Text.Trim(),
                        AdAddress = txtAdAddress.Text.Trim(),
                        AdPic = PicName,
                        AdText = txtAdDetail.Value.Trim(),
                        IsShow = byte.Parse(rblIsShow.SelectedValue),
                        OrderID = int.Parse(txtOrderID.Text.Trim()==""?"0":txtOrderID.Text.Trim()),
                        AdClassID = int.Parse(ddlAdClassID.SelectedValue),
                        AddTime=DateTime.Now
                    }, byte.Parse(rblIsType.SelectedValue));

                if (rows == 1)
                {
                    BasePage.MessageBox("添加成功！", "AdList.aspx");
                }
                else
                { BasePage.MessageBoxGoHistory("添加失败！"); }
                #endregion
            }
        }
    }
}